home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / z150_src.lzh / ZOOLIST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-12  |  11.2 KB  |  347 lines

  1. #ifndef LINT
  2. static char sccsid[]="@(#) zoolist.c 1.12 87/05/29 12:56:18";
  3. #endif /* LINT */
  4.  
  5. /*
  6. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  7. */
  8. #include "options.h"
  9. #include "portable.h"
  10. #include "zoomem.h"  /* to get ZOOCOUNT */
  11.  
  12. /* Lists files in archive */
  13. #include "zoo.h"
  14. #include "errors.i"
  15. #include <stdio.h>
  16. #include "various.h"
  17. #include "zoofns.h"
  18.  
  19. static char tot_fmt[] = "%8lu %3u%% %8lu  %4d file"; 
  20. static char tot_line[] = 
  21.    /* "------------  --------  ---  --------  --------- --------\n"; */
  22.    "--------  --- --------  --------- --------\n";
  23.  
  24. static char dbl_percent[] = "Archive %s:  %s";
  25.  
  26. #ifdef LINT_ARGS
  27. void show_comment (struct direntry *, FILE *, int, char *);
  28. int ver_too_high (struct zoo_header *);
  29. #endif
  30.  
  31. void zoolist (argv, option, argc)
  32. char **argv, *option;
  33. int argc;
  34. {
  35. char whichname[PATHSIZE];  /* which name to use */
  36. char *this_zoo;            /* currently matched archive name */
  37. register FILE *zoo_file;
  38. char *flist[ZOOCOUNT];       /* list of ptrs to input archive names */
  39. int fptr;                  /* will point to within list of archive names */
  40.  
  41. struct direntry direntry;
  42. struct zoo_header zoo_header;
  43. int size_factor;
  44. char packing[9];
  45. unsigned long tot_org_siz = 0L, tot_siz_now = 0L;
  46. int   tot_sf;
  47. int file_count = 0;
  48. int del_count = 0;                  /* number of deleted entries */
  49. int bad_pack;                 /* 1 if packing method is unknown */
  50. static char *month_list="000JanFebMarAprMayJunJulAugSepOctNovDec";
  51. static char dashes[] = "------------\n";
  52. int year, month, day, hours, min, sec;
  53. int list_deleted = 0;         /* list deleted files too */
  54. int fast = 0;                 /* fast list */
  55. int column;                   /* for column printing */
  56. long fiz_ofs = 0;             /* offset where to start */
  57. int verb_list = 0;            /* if verbose listing needed */
  58. int show_name = 0;            /* if archive name to be included in listing */
  59. int zoocount = 1;             /* number of archives to list */
  60. int biglist = 0;              /* multiarchive listing */
  61. int longest;                  /* length of longest archive name */
  62.  
  63. while (*option) {
  64.    switch (*option) {
  65.       case 'a': show_name++; break;
  66.       case 'd': list_deleted++; break;
  67.       case 'f': fast++; break;
  68.       case 'c':
  69.       case 'v': verb_list++; break;
  70.       case 'l': break;
  71.       case 'L': biglist++; zoocount = argc; break;
  72.       case '@': 
  73.          ++option;
  74.          fiz_ofs = calc_ofs(option); 
  75.          goto no_more;
  76.       default:
  77.          prterror ('w', option_ignored, *option);
  78.    }
  79.    option++;
  80. }
  81.  
  82. no_more:  /* come from exit from while loop above */
  83.  
  84. if (fast && show_name) {      /* don't allow 'a' with 'f' */
  85.    show_name = 0;
  86.    prterror ('w', option_ignored, 'a');
  87. }
  88.  
  89. #ifdef WILDCARD
  90.    /* For each archive name supplied, if it is not a char range and
  91.       does not contain a dot, append "*.zoo". */
  92.    {
  93.       int i;
  94.       for (i = 0; i < argc;  i++) {
  95.          if (strchr (nameptr (argv[i]), EXT_CH) == NULL && 
  96.                            !match_half (nameptr (argv[0]), "?-?"))
  97.             argv[i] = newcat (argv[i], "*.zoo");
  98.       }
  99.    }
  100. #endif
  101.  
  102. makelist (zoocount, argv, flist,        ZOOCOUNT-2,   (char *) NULL,".","..", &longest);
  103. /*        ^argc     ^argv ^list_pointer ^max_no_files   ^exclude */
  104.  
  105. for (fptr = 0;  (this_zoo = flist[fptr]) != NULL; fptr++) {
  106.    int ercount;                  /* count of errors */
  107.    int entrycount;               /* count of directory entries */
  108.    int column;                   /* for column printing */
  109.    int expl_deleted;             /* explain what D means */
  110.    int expl_comment;             /* explain what comment means */
  111.    int expl_ver;                 /* Explain what V means */
  112.    int expl_star;                /* Explain what * means */
  113.    int first_time;               /* first time through loop for an archive */
  114.  
  115.    ercount = entrycount = column = del_count =
  116.       expl_deleted = expl_comment = expl_ver = expl_star = 0;
  117.    first_time = 1;
  118.  
  119. #ifndef WILDCARD
  120.    /* Add default extension if none supplied */
  121.    if (strchr (nameptr (this_zoo), EXT_CH) == NULL)
  122.       this_zoo = newcat (this_zoo, EXT_DFLT);
  123. #endif
  124.  
  125.    zoo_file = fopen (this_zoo, FRDSTR);
  126.  
  127.    if (zoo_file == NULL) {
  128.       prterror ('e', could_not_open, this_zoo);
  129.       continue;
  130.    } else if (!show_name)
  131.       printf ("\nArchive %s:\n", this_zoo);
  132.    
  133. if (fiz_ofs != 0L) {                /* if offset specified, start there */
  134.    prterror('m', "Starting at %ld\n", fiz_ofs);
  135.    fseek (zoo_file, fiz_ofs, 0);
  136. } else {
  137.    if (frd_zooh (&zoo_header, zoo_file) == -1 ||
  138.                                              zoo_header.zoo_tag != ZOO_TAG) {
  139.       prterror ('e', dbl_percent, this_zoo, invalid_header);
  140.       goto loop_end;
  141.    }
  142.    /* Seek to the beginning of the first directory entry */
  143.    if (fseek (zoo_file, zoo_header.zoo_start, 0) != 0) {
  144.       ercount++;
  145.       prterror ('e', dbl_percent, this_zoo, bad_directory);
  146.       goto loop_end;
  147.    }
  148.    if (!show_name && ver_too_high (&zoo_header)) {
  149.       ercount++;
  150.       if (ercount < 2) {
  151.          prterror ('w', dbl_percent, this_zoo, "");
  152.          prterror ('M', wrong_version, zoo_header.major_ver, zoo_header.minor_ver);
  153.       }
  154.    }
  155. } /* end if (fiz_ofs !- 0L) */
  156.  
  157.    /* Now we print information about each file in the archive */
  158.    
  159.    if (!show_name) { /* initialize for each file only if not disk catalog */
  160.       tot_org_siz = 0L;  
  161.       tot_siz_now = 0L;
  162.       file_count = 0;
  163.       del_count = 0;
  164.    }
  165.  
  166.    while (1) {
  167.       if (readdir (&direntry, zoo_file, 0) == -1) {
  168.          prterror ('F', dbl_percent, this_zoo, bad_directory);
  169.          goto givesummary;
  170.       }
  171.       if (direntry.zoo_tag != ZOO_TAG) {
  172.          long currpos, zoolength;
  173.          prterror ('F', dbl_percent, this_zoo, invalid_header);
  174.          if ((currpos = ftell (zoo_file)) != -1L)
  175.             if (fseek (zoo_file, 0L, 2) == 0)
  176.                if ((zoolength = ftell (zoo_file)) != -1L)
  177.                   printf (cant_process, zoolength - currpos);              
  178.          goto givesummary;
  179.       }
  180.    
  181.       if (direntry.next == 0L)      /* EXIT on end of chain */
  182.          break;                                 
  183.       else
  184.          entrycount++;              /* Number of directory entries */
  185.    
  186.       if (direntry.deleted)
  187.          ++del_count;
  188.       
  189.         /* Into whichname put the combined pathname + filename */
  190.         strcpy (whichname, fullpath (&direntry));
  191. #ifdef DEBUG
  192.       printf("matching against [%s] and [%s]\n", 
  193.                nameptr(whichname), whichname);
  194. #endif
  195.  
  196.       if ( ( (list_deleted && direntry.deleted) ||
  197.                (list_deleted < 2 && !direntry.deleted)
  198.            ) 
  199.               && (biglist || needed(whichname))) {
  200.    
  201.          file_count++;
  202.    
  203.          if (direntry.packing_method > MAX_PACK) {
  204.             bad_pack = 1;
  205.             expl_ver = 1;
  206.          }  else
  207.             bad_pack = 0;
  208.       
  209.          size_factor = cfactor (direntry.org_size, direntry.size_now);
  210.    
  211.          year  =  ((unsigned int) direntry.date >> 9) & 0x7f;
  212.          month =  ((unsigned int) direntry.date >> 5) & 0x0f;
  213.          day   =  direntry.date        & 0x1f;
  214.    
  215.          hours =  ((unsigned int) direntry.time >> 11)& 0x1f;
  216.          min   =  ((unsigned int) direntry.time >> 5) & 0x3f;
  217.          sec   =  ((unsigned int) direntry.time & 0x1f) * 2;
  218.    
  219.          if (fast) {
  220.             if ((column++ % 5) == 0 && !first_time)
  221.                fputchar ('\n');
  222.             printf("%-12s  ", direntry.fname);
  223.                 fflush (stdout);
  224.    
  225.          } else {
  226.             if (first_time && !show_name) {     /* print archive header */
  227.                printf ("Length    CF  Size Now  Date      Time\n");
  228.                printf (tot_line);
  229.             }
  230.             printf ("%8lu %3u%% %8lu  %2d %-.3s %02d %02d:%02d:%02d",  
  231.                      direntry.org_size, 
  232.                      size_factor, direntry.size_now, 
  233.                      day, &month_list[month*3], 
  234.                      (day && month) ?  (year+80) % 100 : 0,
  235.                      hours, min, sec);
  236.                tot_org_siz += direntry.org_size;
  237.                tot_siz_now += direntry.size_now;
  238.    
  239.             if (direntry.cmt_size) {
  240.                expl_comment++;
  241.                printf ("C");
  242.             } else
  243.                printf (" ");
  244.    
  245.             if (direntry.deleted) {
  246.                expl_deleted++;
  247.                printf ("D");
  248.             }  else
  249.                printf (" ");
  250.             if (list_deleted)
  251.                printf (" ");
  252.             if (show_name)
  253.                printf ("%-*s ", longest, this_zoo);
  254.  
  255.                 /* new code to get around a common compiler bug */
  256.                 printf (whichname);
  257.                 if (direntry.dir_crc != 0) {
  258.                     expl_star++;
  259.                     printf ("*");
  260.                 }
  261.  
  262.             if (bad_pack)
  263.                printf (" (V%d.%d)", direntry.major_ver, direntry.minor_ver);
  264.             printf ("\n");
  265.          }
  266.          first_time = 0;
  267.    
  268.          /* if verbose listing requested show any comment.  f overrrides v */
  269.          if (verb_list && !fast)
  270.             show_comment (&direntry, zoo_file, 0, (char *) NULL);
  271.       }
  272.    
  273.       fseek (zoo_file, direntry.next, 0); /* ..seek to next dir entry */
  274.    } /* end while */
  275.    
  276.    givesummary:
  277.    
  278.    if (!show_name) {
  279.       if (fast) {
  280.          if (file_count) {
  281.             if (del_count)
  282.                printf ("\n-----\n");
  283.             else
  284.                fputchar ('\n');
  285.          }
  286.          if (del_count)
  287.             printf ("%d deleted.\n", del_count);
  288.       }
  289.       if (!fast && file_count) {
  290.          tot_sf = cfactor (tot_org_siz, tot_siz_now);
  291.          printf (tot_line);
  292.       
  293.          printf (tot_fmt, tot_org_siz, tot_sf, tot_siz_now, file_count);
  294.             if (file_count > 1)
  295.                 printf ("s\n");
  296.             else
  297.                 printf ("\n");
  298.          
  299.          if (del_count || expl_ver || expl_deleted || expl_comment)
  300.             printf (dashes);
  301.       }
  302.    
  303.       if (!fast) {
  304.          if (del_count) {
  305.             if (expl_deleted)
  306.                printf ("D: deleted file.\n");
  307.             else {
  308.                if (del_count == 1)
  309.                   printf ("There is 1 deleted file.\n");
  310.                else
  311.                   printf ("There are %d deleted files.\n", del_count);
  312.             }
  313.          }
  314.       }
  315.       if (expl_comment && !fast && !verb_list) 
  316.          printf ("C: file has attached comment.\n");
  317.       if (expl_ver && !fast)
  318.          printf ("V: minimum version of Zoo needed to extract this file.\n");
  319.       if (expl_star && !fast)
  320.          printf ("*: directory entry may be corrupted.\n");
  321.       if (!file_count)
  322.          printf ("Zoo:  %s", no_match);
  323.       
  324.       if (!entrycount && !fiz_ofs)
  325.          printf ("(The archive is empty.)\n");
  326.    } /* end if (show_name) */
  327. loop_end:            /* jump here on badly structured archive */
  328.    fclose (zoo_file);
  329. } /* end for */
  330.  
  331. if (show_name) {
  332.    if (file_count) {
  333.       tot_sf = cfactor (tot_org_siz, tot_siz_now);
  334.       printf (tot_line);
  335.       printf (tot_fmt, tot_org_siz, tot_sf, tot_siz_now, file_count);
  336.         if (file_count > 1)
  337.             printf ("s\n");
  338.         else
  339.             printf ("\n");
  340.    } 
  341. }
  342.  
  343. if (!file_count)
  344.    exit (1);            /* Consider it an error if there were no files */
  345. } /* zoolist() */
  346.  
  347.